home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: "free"ing classes when the "new" is inside a function
- Message-ID: <marnoldDoCs2M.E00@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4ic7a5$94u@falcon.ccs.uwo.ca> <3149B5B8.5C25@bnr.ca> <4icna3$7p3@falcon.ccs.uwo.ca>
- Date: Sat, 16 Mar 1996 09:08:46 GMT
- Sender: marnold@netcom2.netcom.com
-
- turnbull@canlon.physics.uwo.ca (David Turnbull) writes:
-
- >Joseph Bell <jobell@bnr.ca> wrote:
-
- [snip]
-
- >>What is the difference between MyClass and Class?
-
- >>Consider:
-
- >>A* returnPtrtoA()
- >>{
- >> A* returnPtr = new A(...);
- >> return returnPtr;
- >>}
-
- >>void main (void)
- >>{
- >> A* myAPtr = returnPtrtoA();
- >>
-
- >> delete myAPtr; // frees object and calls destructor
- >>}
-
- >>That works.
-
- >Sorry, I got careless in my example. Class and MyClass were supposed
- >to be the same. In other words, like your example. However, this does
- >NOT free up the memory!
- >Or perhaps my simplification of the problem is hiding something. What
- >I have is closer to
-
-
- >MyClass* Level1()
- >{
- > MyClass* result;
- > result=Level2();
- > return result;
- >}
-
- >MyClass* Level2()
- >{
- > MyClass* result;
- > result=Level3();
- > return result;
- >}
-
- >MyClass* Level3()
- >{
- > MyClass* result;
- > result=new MyClass(..........);
- > return result;
- >}
-
- >void main(void)
- >{
-
- > MyClass* value;
- > value=Level1();
- > delete value;
-
- >}
-
- >In this case the delete is not freeing memory.
-
- It certainly should! delete does not care where the new occured. Your
- code allocated a MyClass object and stores it in a MyClass* variable,
- eventually using such a varaible to delete the allocated MyClass object.
- Perfectly wonderful C++.
-
- What means are you using to determine that memory is not being freed?
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-